package com.pansoft.mobilemallpay.pays;

import android.app.Activity;
import android.content.Context;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;

import com.alipay.sdk.app.PayTask;
import com.efounder.http.UniversalRequestManager;
import com.efounder.util.DialogUtils;
import com.efounder.utils.ResStringUtil;
import com.google.gson.Gson;
import com.pansoft.mobilemallpay.R;
import com.pansoft.mobilemallpay.bean.PayOrderResultBean;
import com.pansoft.mobilemallpay.bean.PayResult;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by meijun on 17-11-28.
 */

public class AliPay implements PayInter {


    private String url;
    private HashMap paramsMap;

    public AliPay(Context mContext) {
        this.mContext = mContext;
    }

    private Context mContext;



    @Override
    public void setURLAndParams(String url, HashMap paramsMap) {
        this.url = url;
        this.paramsMap = paramsMap;
        paramsMap.put("dev","android");
    }

    /**
     * 支付
     *
     * @param payListener 请求回调
     */
    @Override
    public void pay(final OnPayListener payListener) {
        DialogUtils.showProgressDialog(ResStringUtil.getString(R.string.common_text_please_wait));
        UniversalRequestManager.getInstance(mContext).
                requestAsyn(url,
                        1, paramsMap, new UniversalRequestManager.ReqCallBack<Object>() {
                            @Override
                            public void onReqSuccess(Object result) {
                                DialogUtils.dismissProgressDialog();
                                Gson gson = new Gson();
                                try {
                                    PayOrderResultBean payOrderResultBean = gson.fromJson(result.toString(), PayOrderResultBean.class);
                                    if (payOrderResultBean != null && "success".equals(payOrderResultBean.getResult())) {
                                   /*     Map<String, String> params = OrderInfoUtil2_0.buildOrderParamMap(payOrderResultBean.getAppid(), true);
                                        String orderParam = OrderInfoUtil2_0.buildOrderParam(params);
                                        String privateKey = payOrderResultBean.getPrivatekey();
                                        String sign = OrderInfoUtil2_0.getSign(params, privateKey, true);
                                        final String orderInfo = orderParam + "&" + sign;*/
                                        //去支付
                                        final String orderInfo = payOrderResultBean.getMsg();
                                        Runnable payRunnable = new Runnable() {

                                            @Override
                                            public void run() {
                                                PayTask alipay = new PayTask((Activity) mContext);
                                                Map<String, String> result = alipay.payV2(orderInfo, true);
                                                Log.i("msp", result.toString());
                                                Message msg = new Message();
                                                notifyResult(result, payListener, mContext);
                                            }
                                        };

                                        Thread payThread = new Thread(payRunnable);
                                        payThread.start();

                                    } else {
                                        onReqFailed(ResStringUtil.getString(R.string.common_text_pay_request_prepayment_failed));
                                    }
                                } catch (Exception e) {
                                    e.printStackTrace();
                                    onReqFailed(ResStringUtil.getString(R.string.common_text_pay_request_prepayment_failed));
                                }
                            }

                            @Override
                            public void onReqFailed(String errorMsg) {
                                DialogUtils.dismissProgressDialog();
                                    payListener.onPayFailure(errorMsg,OnPayListener.ERROR);
                            }
                        });

    }

    private void notifyResult(Map<String, String> result, final OnPayListener payListener, Context mContext) {
        @SuppressWarnings("unchecked")
        PayResult payResult = new PayResult((Map<String, String>) result);
        /**
         对于支付结果，请商户依赖服务端的异步通知结果。同步通知结果，仅作为支付结束的通知。
         */
        String resultInfo = payResult.getResult();// 同步返回需要验证的信息
        final String resultStatus = payResult.getResultStatus();

        Activity activity = mContext instanceof Activity ? ((Activity) mContext) : null;
        if (activity != null) {
            activity.runOnUiThread(new Runnable() {
                @Override
                public void run() {

                    if (TextUtils.equals(resultStatus, "9000")) {
                        payListener.onPaySuccess();
                    } else {
                        payListener.onPayFailure(ResStringUtil.getString(R.string.common_text_pay_fail),OnPayListener.ERROR);
                    }

                }
            });
        }else {

            if (TextUtils.equals(resultStatus, "9000")) {
                payListener.onPaySuccess();
            } else {
                payListener.onPayFailure(ResStringUtil.getString(R.string.common_text_pay_fail),OnPayListener.ERROR);
            }

        }
        // 判断resultStatus 为9000则代表支付成功

    }
}

